namespace NCT.MRS.DataAccessLayer.Migrations
{
    using System;
    using System.Data.Entity.Migrations;
    
    public partial class AddInitialCreate : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Users",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        FirstName = c.String(nullable: false, maxLength: 30),
                        LastName = c.String(nullable: false, maxLength: 30),
                        MiddleName = c.String(maxLength: 30),
                        Username = c.String(nullable: false, maxLength: 30),
                        Password = c.String(nullable: false, maxLength: 100),
                        IsActive = c.Boolean(nullable: false,defaultValue:true),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        RoleId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Roles", t => t.RoleId, cascadeDelete: true)
                .Index(t => t.RoleId);
            
            CreateTable(
                "dbo.Roles",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 30),
                        Description = c.String(maxLength: 100),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.AccessTypes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(nullable: false, maxLength: 30),
                        Name = c.String(nullable: false, maxLength: 30),
                        Description = c.String(maxLength: 100),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        AccessSectionId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.AccessSections", t => t.AccessSectionId, cascadeDelete: true)
                .Index(t => t.AccessSectionId);
            
            CreateTable(
                "dbo.AccessSections",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(nullable: false, maxLength: 30),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                    })
                .PrimaryKey(t => t.Id);
            
            CreateTable(
                "dbo.Clients",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        RegistrationId = c.String(nullable: false, maxLength: 50),
                        FirstName = c.String(nullable: false, maxLength: 30),
                        LastName = c.String(nullable: false, maxLength: 30),
                        MiddleName = c.String(maxLength: 30),
                        Gender = c.Int(nullable: false),
                        Phone = c.String(maxLength: 4000),
                        Address = c.String(maxLength: 4000),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        IsSenior = c.Boolean(nullable: false, defaultValue: false),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        ImagePath = c.String(maxLength: 4000),
                        ModifiedByUserId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .Index(t => t.ModifiedByUserId);
            
            CreateTable(
                "dbo.TestRequests",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        TestRequestNumber = c.String(maxLength: 4000),
                        Discount = c.Decimal(nullable: false, precision: 18, scale: 2),
                        DiscountPrice = c.Decimal(nullable: false, precision: 18, scale: 2),
                        Total = c.Decimal(nullable: false, precision: 18, scale: 2),
                        SubTotal = c.Decimal(nullable: false, precision: 18, scale: 2),
                        DoctorId = c.Int(nullable: false),
                        ClientId = c.Int(nullable: false),
                        TestPackageId = c.Int(),
                        PackageDiscount = c.Decimal(nullable: false, precision: 18, scale: 2),
                        IsVoided = c.Boolean(nullable: false, defaultValue: false),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        ModifiedByUserId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Doctors", t => t.DoctorId, cascadeDelete: true)
                .ForeignKey("dbo.TestPackages", t => t.TestPackageId)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .ForeignKey("dbo.Clients", t => t.ClientId, cascadeDelete: true)
                .Index(t => t.DoctorId)
                .Index(t => t.TestPackageId)
                .Index(t => t.ModifiedByUserId)
                .Index(t => t.ClientId);
            
            CreateTable(
                "dbo.Doctors",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 4000),
                        FirstName = c.String(nullable: false, maxLength: 30),
                        MiddleName = c.String(maxLength: 30),
                        LastName = c.String(nullable: false, maxLength: 30),
                        Phone = c.String(maxLength: 4000),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        ModifiedByUserId = c.Int(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .Index(t => t.ModifiedByUserId);
            
            CreateTable(
                "dbo.TestPackages",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(maxLength: 4000),
                        Name = c.String(nullable: false, maxLength: 30),
                        Description = c.String(maxLength: 4000),
                        Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        ModifiedByUserId = c.Int(nullable: false),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .Index(t => t.ModifiedByUserId);
            
            CreateTable(
                "dbo.TestTypes",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(nullable: false, maxLength: 30),
                        Name = c.String(nullable: false, maxLength: 30),
                        NormalResult = c.String(maxLength: 250),
                        Price = c.Decimal(nullable: false, precision: 18, scale: 2),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        ModifiedByUserId = c.Int(nullable: false),
                        SectionId = c.Int(nullable: false),
                        ParentTestId = c.Int(),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .ForeignKey("dbo.TestTypeSections", t => t.SectionId, cascadeDelete: true)
                .ForeignKey("dbo.TestTypes", t => t.ParentTestId)
                .Index(t => t.ModifiedByUserId)
                .Index(t => t.SectionId)
                .Index(t => t.ParentTestId);
            
            CreateTable(
                "dbo.TestTypeSections",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Code = c.String(nullable: false, maxLength: 30),
                        Name = c.String(nullable: false, maxLength: 30),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                        ModifiedByUserId = c.Int(nullable: false),
                        DateAdded = c.DateTime(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .Index(t => t.ModifiedByUserId);
            
            CreateTable(
                "dbo.TestTypeResults",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        TestTypeId = c.Int(nullable: false),
                        TestRequestId = c.Int(nullable: false),
                        Result = c.String(maxLength: 250),
                        ModifiedByUserId = c.Int(nullable: false),
                        DateModified = c.DateTime(nullable: false),
                        IsActive = c.Boolean(nullable: false, defaultValue: true),
                    })
                .PrimaryKey(t => t.Id)
                .ForeignKey("dbo.Users", t => t.ModifiedByUserId)
                .ForeignKey("dbo.TestTypes", t => t.TestTypeId, cascadeDelete: true)
                .ForeignKey("dbo.TestRequests", t => t.TestRequestId, cascadeDelete: true)
                .Index(t => t.ModifiedByUserId)
                .Index(t => t.TestTypeId)
                .Index(t => t.TestRequestId);
            
            CreateTable(
                "dbo.AccessTypeRoles",
                c => new
                    {
                        AccessType_Id = c.Int(nullable: false),
                        Role_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.AccessType_Id, t.Role_Id })
                .ForeignKey("dbo.AccessTypes", t => t.AccessType_Id, cascadeDelete: true)
                .ForeignKey("dbo.Roles", t => t.Role_Id, cascadeDelete: true)
                .Index(t => t.AccessType_Id)
                .Index(t => t.Role_Id);
            
            CreateTable(
                "dbo.TestTypeTestPackages",
                c => new
                    {
                        TestType_Id = c.Int(nullable: false),
                        TestPackage_Id = c.Int(nullable: false),
                    })
                .PrimaryKey(t => new { t.TestType_Id, t.TestPackage_Id })
                .ForeignKey("dbo.TestTypes", t => t.TestType_Id, cascadeDelete: true)
                .ForeignKey("dbo.TestPackages", t => t.TestPackage_Id, cascadeDelete: true)
                .Index(t => t.TestType_Id)
                .Index(t => t.TestPackage_Id);
            
        }
        
        public override void Down()
        {
            DropIndex("dbo.TestTypeTestPackages", new[] { "TestPackage_Id" });
            DropIndex("dbo.TestTypeTestPackages", new[] { "TestType_Id" });
            DropIndex("dbo.AccessTypeRoles", new[] { "Role_Id" });
            DropIndex("dbo.AccessTypeRoles", new[] { "AccessType_Id" });
            DropIndex("dbo.TestTypeResults", new[] { "TestRequestId" });
            DropIndex("dbo.TestTypeResults", new[] { "TestTypeId" });
            DropIndex("dbo.TestTypeResults", new[] { "ModifiedByUserId" });
            DropIndex("dbo.TestTypeSections", new[] { "ModifiedByUserId" });
            DropIndex("dbo.TestTypes", new[] { "ParentTestId" });
            DropIndex("dbo.TestTypes", new[] { "SectionId" });
            DropIndex("dbo.TestTypes", new[] { "ModifiedByUserId" });
            DropIndex("dbo.TestPackages", new[] { "ModifiedByUserId" });
            DropIndex("dbo.Doctors", new[] { "ModifiedByUserId" });
            DropIndex("dbo.TestRequests", new[] { "ClientId" });
            DropIndex("dbo.TestRequests", new[] { "ModifiedByUserId" });
            DropIndex("dbo.TestRequests", new[] { "TestPackageId" });
            DropIndex("dbo.TestRequests", new[] { "DoctorId" });
            DropIndex("dbo.Clients", new[] { "ModifiedByUserId" });
            DropIndex("dbo.AccessTypes", new[] { "AccessSectionId" });
            DropIndex("dbo.Users", new[] { "RoleId" });
            DropForeignKey("dbo.TestTypeTestPackages", "TestPackage_Id", "dbo.TestPackages");
            DropForeignKey("dbo.TestTypeTestPackages", "TestType_Id", "dbo.TestTypes");
            DropForeignKey("dbo.AccessTypeRoles", "Role_Id", "dbo.Roles");
            DropForeignKey("dbo.AccessTypeRoles", "AccessType_Id", "dbo.AccessTypes");
            DropForeignKey("dbo.TestTypeResults", "TestRequestId", "dbo.TestRequests");
            DropForeignKey("dbo.TestTypeResults", "TestTypeId", "dbo.TestTypes");
            DropForeignKey("dbo.TestTypeResults", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.TestTypeSections", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.TestTypes", "ParentTestId", "dbo.TestTypes");
            DropForeignKey("dbo.TestTypes", "SectionId", "dbo.TestTypeSections");
            DropForeignKey("dbo.TestTypes", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.TestPackages", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.Doctors", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.TestRequests", "ClientId", "dbo.Clients");
            DropForeignKey("dbo.TestRequests", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.TestRequests", "TestPackageId", "dbo.TestPackages");
            DropForeignKey("dbo.TestRequests", "DoctorId", "dbo.Doctors");
            DropForeignKey("dbo.Clients", "ModifiedByUserId", "dbo.Users");
            DropForeignKey("dbo.AccessTypes", "AccessSectionId", "dbo.AccessSections");
            DropForeignKey("dbo.Users", "RoleId", "dbo.Roles");
            DropTable("dbo.TestTypeTestPackages");
            DropTable("dbo.AccessTypeRoles");
            DropTable("dbo.TestTypeResults");
            DropTable("dbo.TestTypeSections");
            DropTable("dbo.TestTypes");
            DropTable("dbo.TestPackages");
            DropTable("dbo.Doctors");
            DropTable("dbo.TestRequests");
            DropTable("dbo.Clients");
            DropTable("dbo.AccessSections");
            DropTable("dbo.AccessTypes");
            DropTable("dbo.Roles");
            DropTable("dbo.Users");
        }
    }
}
